import { _decorator } from 'cc';
import { Block } from './Block';
import { GridManager } from './GridManager';

const { ccclass } = _decorator;

enum BlockType {
    Normal,
    Large,
    HorizontalStripe,
    VerticalStripe,
    ColorBomb,
    // Add more types as needed
}

interface IBlock {
    type: BlockType;
    gridPosition: { x: number, y: number };
    canEliminate(): boolean;
    eliminate(): void;
    executeEffect(): void;
}

@ccclass('EliminateManager')
export class EliminateManager {
    private gridManager: GridManager;
    private readonly MIN_ELIMINATE_COUNT = 3; // 最小消除数量

    constructor(gridManager: GridManager) {
        this.gridManager = gridManager;
    }

    // 检查整个网格的消除
    checkAllEliminations(): boolean {
        const eliminateBlocks = new Set<Block>();
        
        // 检查横向消除
        for (let y = 0; y < this.gridManager.getHeight(); y++) {
            this.checkRowElimination(y, eliminateBlocks);
        }

        // 检查纵向消除
        for (let x = 0; x < this.gridManager.getWidth(); x++) {
            this.checkColumnElimination(x, eliminateBlocks);
        }

        // 执行消除
        if (eliminateBlocks.size > 0) {
            this.eliminateBlocks(eliminateBlocks);
            return true;
        }

        return false;
    }

    // 检查横向消除
    private checkRowElimination(row: number, eliminateBlocks: Set<Block>) {
        let start = 0;
        while (start < this.gridManager.getWidth()) {
            const sameTypeBlocks = this.findSameTypeBlocksInRow(row, start);
            if (sameTypeBlocks.length >= this.MIN_ELIMINATE_COUNT) {
                sameTypeBlocks.forEach(block => eliminateBlocks.add(block));
            }
            start += sameTypeBlocks.length;
        }
    }

    // 检查纵向消除
    private checkColumnElimination(col: number, eliminateBlocks: Set<Block>) {
        let start = 0;
        while (start < this.gridManager.getHeight()) {
            const sameTypeBlocks = this.findSameTypeBlocksInColumn(col, start);
            if (sameTypeBlocks.length >= this.MIN_ELIMINATE_COUNT) {
                sameTypeBlocks.forEach(block => eliminateBlocks.add(block));
            }
            start += sameTypeBlocks.length;
        }
    }

    // 查找同一行中相同类型的方块
    private findSameTypeBlocksInRow(row: number, startCol: number): Block[] {
        const sameBlocks: Block[] = [];
        let currentType: number | null = null;

        for (let x = startCol; x < this.gridManager.getWidth(); x++) {
            const block = this.gridManager.getBlock(x, row)?.getComponent(Block);
            if (!block || !block.canEliminate()) break;

            if (currentType === null) {
                currentType = block.type;
                sameBlocks.push(block);
            } else if (block.type === currentType) {
                sameBlocks.push(block);
            } else {
                break;
            }
        }

        return sameBlocks;
    }

    // 查找同一列中相同类型的方块
    private findSameTypeBlocksInColumn(col: number, startRow: number): Block[] {
        const sameBlocks: Block[] = [];
        let currentType: number | null = null;

        for (let y = startRow; y < this.gridManager.getHeight(); y++) {
            const block = this.gridManager.getBlock(col, y)?.getComponent(Block);
            if (!block || !block.canEliminate()) break;

            if (currentType === null) {
                currentType = block.type;
                sameBlocks.push(block);
            } else if (block.type === currentType) {
                sameBlocks.push(block);
            } else {
                break;
            }
        }

        return sameBlocks;
    }

    // 执行消除
    private eliminateBlocks(blocks: Set<Block>) {
        blocks.forEach(block => {
            block.executeEffect(); // 执行特殊效果
            block.eliminate(); // 消除方块
        });
    }
} 